﻿@page
@model Net8.Identity.Server.Pages.LogoutModel
@{
    ViewData["Title"] = "登出确认";
}

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6 col-lg-5">
            <div class="card-modern mt-5">
                <div class="card-header" style="background: linear-gradient(135deg, var(--warning-color) 0%, #f59e0b 100%);">
                    <h3 class="mb-2">
                        <i class="bi bi-box-arrow-right me-2"></i>登出确认
                    </h3>
                    <p class="mb-0">请确认您是否要登出当前账户</p>
                </div>
                
                <div class="card-body text-center">
                    <div class="mb-4">
                        <div class="icon icon-warning mx-auto mb-3" style="width: 4rem; height: 4rem; font-size: 1.5rem;">
                            <i class="bi bi-question-circle"></i>
                        </div>
                        <h5 class="fw-bold mb-3">确认登出？</h5>
                        <p class="text-muted">
                            登出后，您的当前会话将被清除，<br>
                            所有访问令牌将失效，需要重新登录。
                        </p>
                    </div>

                    <div class="alert alert-warning alert-modern mb-4">
                        <h6 class="alert-heading">
                            <i class="bi bi-exclamation-triangle me-1"></i>注意事项
                        </h6>
                        <ul class="mb-0 text-start small">
                            <li>登出后您需要重新输入凭据才能访问系统</li>
                            <li>正在进行的操作可能会丢失，请先保存数据</li>
                            <li>如果是公共设备，建议登出以保护账户安全</li>
                        </ul>
                    </div>

                    <form id="logoutForm" action="/oauth/logout" method="post">
                        @Html.AntiForgeryToken()
                        
                        @* 传递OAuth登出需要的关键参数 *@
                        @if (!string.IsNullOrEmpty(HttpContext.Request.Query["client_id"]))
                        {
                            <input type="hidden" name="client_id" value="@HttpContext.Request.Query["client_id"]" />
                        }
                        @if (!string.IsNullOrEmpty(HttpContext.Request.Query["post_logout_redirect_uri"]))
                        {
                            <input type="hidden" name="post_logout_redirect_uri" value="@HttpContext.Request.Query["post_logout_redirect_uri"]" />
                        }
                        @if (!string.IsNullOrEmpty(HttpContext.Request.Query["id_token_hint"]))
                        {
                            <input type="hidden" name="id_token_hint" value="@HttpContext.Request.Query["id_token_hint"]" />
                        }
                        @if (!string.IsNullOrEmpty(HttpContext.Request.Query["state"]))
                        {
                            <input type="hidden" name="state" value="@HttpContext.Request.Query["state"]" />
                        }
                        
                        <div class="row g-2">
                            <div class="col-6">
                                <button type="submit" name="Confirm" value="Yes" class="btn btn-danger btn-modern w-100" id="logoutBtn">
                                    <i class="bi bi-box-arrow-right me-2"></i>确认登出
                                </button>
                            </div>
                            <div class="col-6">
                                <a href="/" class="btn btn-outline-secondary btn-modern w-100">
                                    <i class="bi bi-x-lg me-2"></i>取消
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                
                <div class="card-footer text-center" style="background: var(--gray-50); border-top: 1px solid var(--gray-200); padding: 1rem;">
                    <small class="text-muted">
                        <i class="bi bi-shield-check me-1"></i>
                        为了您的账户安全，请妥善保管您的登录信息
                    </small>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const form = document.getElementById('logoutForm');
            const logoutBtn = document.getElementById('logoutBtn');
            
            if (form && logoutBtn) {
                // 监听表单提交事件
                form.addEventListener('submit', function(e) {
                    // 允许表单正常提交，不阻止默认行为
                });
                
                logoutBtn.addEventListener('click', function(e) {
                    // 在禁用按钮之前，先添加隐藏字段确保Confirm值能被提交
                    const form = this.closest('form');
                    if (form && !form.querySelector('input[name="Confirm"][type="hidden"]')) {
                        const hiddenConfirm = document.createElement('input');
                        hiddenConfirm.type = 'hidden';
                        hiddenConfirm.name = 'Confirm';
                        hiddenConfirm.value = 'Yes';
                        form.appendChild(hiddenConfirm);
                    }
                    
                    // 更改按钮状态
                    this.innerHTML = '<i class="bi bi-hourglass-split me-2"></i>登出中...';
                    this.disabled = true;
                    
                    // 手动触发表单提交
                    setTimeout(() => {
                        form.submit();
                    }, 100); // 短暂延迟确保DOM更新完成
                });
            }
        });
    </script>
}